package com.typing.battle.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.util.Log;

public class KDialog extends Activity{
    
	private AlertDialog dialog = null;
	private ProgressDialog progressDialog = null;
	
	
	public static int TYPE_NONE = 0;
	public static int TYPE_ROOM_EXIT = 10;
	
	public String getTitle(Context context) {
    	return context.getString(android.R.string.dialog_alert_title);
    }
    
    /** 
     * Dialog Alert
     */
    public boolean showAlert(Context context, String message) {    	    	
    	return showAlert(context, getTitle(context) , message,false,TYPE_NONE);
    }    
    /**
     * Dialog Alert
     */
    public boolean showAlert(Context context, int message) {
    	return showAlert(context,android.R.string.dialog_alert_title , message, false);
    }    
    /**
     * Dialog Alert
     */
    public boolean showAlert(Context context, String message , final boolean cancelable) {
    	return showAlert(context,getTitle(context) , message,cancelable ,TYPE_NONE);
    }
    /**
     * Dialog Alert
     */
    public boolean showAlert(Context context, int message , final boolean cancelable) {
    	return showAlert(context,android.R.string.dialog_alert_title , message,cancelable);
    }
    /**
     * Dialog Alert
     */
    public boolean showAlert(Context context,int title, int message) {
    	return showAlert(context,title, message ,false);
    }
    /**
     * Dialog Alert
     */
    public boolean showAlert(Context context,String title, String message) {
    	return showAlert(context,title , message,false, TYPE_NONE);
    }
    /**
     * Dialog Alert 
     */
    public boolean showAlert(Context context,int title, int message ,final boolean cancelable) {
        return showAlert(context, context.getString(title) , context.getString(message), cancelable , TYPE_NONE);
    }    
    /**
     * Dialog Alert 
     */
    public boolean showAlert(Context context,int title, int message ,final boolean cancelable,final int type) {
    	return showAlert(context, context.getString(title) , context.getString(message), cancelable , type);
    }    
    /**
     * Dialog Alert
     */
    public boolean showAlert(final Context context,String title, String message, final boolean cancelable,final int type) {
        try{
        	closeProgress(context);
        	closeDialog(context);
	        dialog =new AlertDialog.Builder(context)
	        .setTitle(title)
	        .setMessage(message)
	        .setIcon(android.R.drawable.ic_dialog_alert)
	        .setCancelable(cancelable)
	        .setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
							dialogCancel(type);
				}
			})
	        .setPositiveButton(android.R.string.ok,
	                new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	dialogPositive(type);
	                    }
	                }).show();
        }catch (Exception e) {
        	Log.e("", e.toString(), e);
		}
        return true;
    }
    
    /** 
     * Dialog Confirm
     */
    public boolean showConfirm(Context context, String message) {    	    	
    	return showConfirm(context, getTitle(context) , message,false,TYPE_NONE);
    }    
    /**
     * Dialog Confirm
     */
    public boolean showConfirm(Context context, int message) {
    	return showConfirm(context,android.R.string.dialog_alert_title , message, false);
    }    
    /**
     * Dialog Confirm
     */
    public boolean showConfirm(Context context, String message , final boolean cancelable) {
    	return showConfirm(context,getTitle(context) , message,cancelable ,TYPE_NONE);
    }
    /**
     * Dialog Confirm
     */
    public boolean showConfirm(Context context, int message , final boolean cancelable) {
    	return showConfirm(context,android.R.string.dialog_alert_title , message,cancelable);
    }
    /**
     * Dialog Confirm
     */
    public boolean showConfirm(Context context,int title, int message) {
    	return showConfirm(context,title, message ,false);
    }
    /**
     * Dialog Confirm
     */
    public boolean showConfirm(Context context,String title, String message) {
    	return showConfirm(context,title , message,false, TYPE_NONE);
    }
    /**
     * Dialog Confirm 
     */
    public boolean showConfirm(Context context,int title, int message ,final boolean cancelable) {
        return showConfirm(context, context.getString(title) , context.getString(message), cancelable , TYPE_NONE);
    }    
    /**
     * Dialog Confirm 
     */
    public boolean showConfirm(Context context,int message ,final boolean cancelable,final int type) {
    	return showConfirm(context, getTitle(context) , context.getString(message), cancelable , type);
    }   
    /**
     * Dialog Confirm 
     */
    public boolean showConfirm(Context context,String message ,final boolean cancelable,final int type) {
    	return showConfirm(context, getTitle(context) , message, cancelable , type);
    }   
    /**
     * Dialog Confirm 
     */
    public boolean showConfirm(Context context,int title, int message ,final boolean cancelable,final int type) {
    	return showConfirm(context, context.getString(title) , context.getString(message), cancelable , type);
    }   
    
    /**
     * Dialog Confirm 
     */
    public boolean showConfirm(final Context context,String title,String message , final boolean cancelable ,final int type) {
        try{
        	closeProgress(context);
        	closeDialog(context);
        	dialog =new AlertDialog.Builder(context)
        	.setTitle(title)
	        .setMessage(message)
	        .setCancelable(cancelable)
	        .setIcon(android.R.drawable.ic_dialog_alert)
	        .setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
							dialogCancel(type);
				}
			})
            .setNegativeButton(android.R.string.cancel, 
	                    new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int which) {
	                		dialog.dismiss();
	                		dialogNegative(type);
	                }
	            })
	        .setPositiveButton(android.R.string.ok,
	                new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                        dialog.dismiss();
	                        dialogPositive(type);
	                    }
	                }).show();
        }catch (Exception e) {
        	Log.e("", e.toString(), e);
		}
        return true;
    }
    
	    
//    public boolean showSelectCity(final WeatherInfoActivity mActivity){
//    	final String[] cityList = mActivity.getResources().getStringArray(R.array.city_name_array);
//    	final String[] cityListKo = mActivity.getResources().getStringArray(R.array.city_name_array_ko);
//    	try{
//    		closeProgress(mActivity);
//    		closeDialog(mActivity);
//    		
//    		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
//    		builder.setTitle(R.string.city_select_popup_title);
//        	builder.setSingleChoiceItems(cityListKo, 0, new DialogInterface.OnClickListener() {
//				
//				public void onClick(DialogInterface dialog, int which) {
//					KMPreference.getInstance(mActivity).setData(KMPreference.SELECTED_CITY, cityList[which]);
//					KMPreference.getInstance(mActivity).setData(KMPreference.SELECTED_CITY_KO, cityListKo[which]);
//					new WeatherDataAsync(mActivity).execute();
//					dialog.dismiss();
//				}
//			})
//        	.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
//				
//				@Override
//				public void onClick(DialogInterface dialog, int which) {
//					dialog.dismiss();
//				}
//			});  	
//			dialog = builder.show();
//			
//    	}catch (Exception e) {
//    		Log.e("", e.toString(), e);
//    	}
//    	
//    	return true;
//    }
    
	
    public void showProgress(Context context){
    	try{
    		if(progressDialog!=null){
    			progressDialog.cancel();
    			progressDialog.dismiss();
    		}
    			
    		//else
    		progressDialog = new ProgressDialog(context);
    		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    		progressDialog.setMessage("Loding...");
    		progressDialog.show();
    	}catch (Exception e) {
    		Log.e("", e.toString(), e);
		}
	}
    
    public void closeDialog(Context context){
		try {
			if (dialog != null){ 
//				dialog.cancel();
				dialog.dismiss();
			}
			dialog = null;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
		}

    }
    
    public AlertDialog getDialog() {
    	return dialog;
    }
    
    public void closeProgress(Context context){
		try {
	    	if(progressDialog!=null){
//	    		progressDialog.cancel();
	    		progressDialog.dismiss();
	    	}
	    	progressDialog = null;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
		}
    }
    public void dialogPositive(final int type){
    	
    }
    public void dialogNegative(final int type){
    	
    }
    public void dialogCancel(final int type){
    	
    }
	private void cleanUp(){
//		BookshelfActivity.cleanUp();	//bookshelf clean up ;
	}
}
